﻿using Leeder.Logger;
using SPSAndong.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SPSAndong.Business
{
    /// <summary>
    /// UPS状态判断
    /// </summary>
    public class UpsBusiness
    {
        /// <summary>
        /// 日志
        /// </summary>
        LogOperate LogObj => Log.GetLogger(this.GetType());

        private static string lastException = ""; //记录上一次异常信息

        /// <summary>
        /// 检查UPS状态数据
        /// </summary>
        /// <param name="model"></param>
        public static void CheckUps(UpsModel model)
        {
            if (model.State != null && model.QueryResult)
            {
                if (model.State.UpsFault == 0)
                {
                    if (model.State.BatteryVoltage == 1)
                    {
                        //电池电压低，相同的异常在没有变化的情况下只记录一次异常
                        if (lastException != "电池电压低")
                        {
                            ExceptionSaveUps("电池电压低");
                            lastException = "电池电压低";
                        }
                    }
                    else
                    {
                        //电池正常
                        lastException = "";
                    }
                }
                else
                {
                    //UPS故障，相同的异常在没有变化的情况下只记录一次异常
                    if (lastException != "UPS故障")
                    {
                        ExceptionSaveUps("UPS故障");
                        lastException = "UPS故障";
                    }
                }
            }
            else
            {
                //其他异常，相同的异常在没有变化的情况下只记录一次异常
                if (lastException != model.ErrorMsg)
                {
                    ExceptionSaveUps(model.ErrorMsg);
                    lastException = model.ErrorMsg;
                }
            }
        }

        public static void ExceptionSaveUps(string exceptionMasg)
        {
            //LogObj.Error("LIP连接异常");
        }
    }
}
